package com.lzp.service.impl;

import com.github.pagehelper.PageHelper;
import com.lzp.mapper.SysUserMapper;
import com.lzp.mapper.SysUserMapperCustom;
import com.lzp.pojo.SysUser;
import com.lzp.service.UserService;
import com.lzp.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @description: 用户service实现类
 * @author: lzp
 * @createTime: 2021-08-03 22:21
 **/
@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserMapperCustom userMapperCustom;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUser(SysUser user) {
        userMapper.insert(user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUser(SysUser user) {
        // 仅更新有值的字段
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUser(String userId) {
        userMapper.deleteByPrimaryKey(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public SysUser queryUserById(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> queryUserList(SysUser user) {
        // 条件
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("username", "%" + user.getUsername() + "%");

        return userMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> queryUserListPaged(SysUser user, Integer page, Integer pageSize) {

        PageHelper.startPage(page, pageSize);

        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andLike("username", "%" + user.getUsername() + "%");

        example.orderBy("registTime").desc();

        return userMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryUserListPagedGrid(SysUser user, Integer page, Integer pageSize) {
        List<SysUser> userList = queryUserListPaged(user, page, pageSize);
        return wrapPagedGrid(userList, page);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public SysUser queryUserByIdCustom(String userId) {
        return userMapperCustom.queryUserByIdCustom(userId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUserTransactional(SysUser user) {
        userMapper.insert(user);

        int i = 1 / 0;

        user.setIsDelete(1);
        userMapper.updateByPrimaryKeySelective(user);
    }
}
